
var postCommon = function () {
    checkLogin(function () {
        Prepare({
            complete: function (location) {
                var postTitle = $('textarea').val().trim(),
                    images = $('.uploadfuck').find('img').map(function (i, v) {
                        return v.src
                    }).toArray().toString();
                
                $.ajax({
                    url: baseurl + '/post/publish/common.do',
                    dataType: 'jsonp',
                    data: {
                        postTitle: postTitle,
                        poiId: location.poiId,
                        images: images
                    }
                }).then(function (data) {
                    ensureCorrectRes(data, function () {
                        AlertBox({
                            msg: '发布成功',
                            type: 'mini',
                        })
                    })
                })
            }
        })

    })

}

var checkImage = function () {
    var all = true
    var imgs = $('img');
    for (var i = 0; i < imgs.length; i++) {
        if (!$(imgs[i]).attr('src')) {
            $(imgs[i]).hide();
            all = false;
        }
    }
    if (all) {
        $('.upload-text').hide();
    }
    else {
        $('.upload-text').show();
    }
}

$(function () {
    $('textarea').on('input', function () {
        var text = $(this).val()
        if (text.length) {
            $('.send-post').css('background', '#fa0')
        } else {
            $('.send-post').attr('style', '')
        }

    });
    $('.send-post').click(function () {
        if (!$('textarea').val().trim()) {
            return;
        }
        postCommon()
    });
    checkImage();
    //  $('#filechooser').on('change', uploadCallback)
    checkLogin();
    var domain_name = (env == 'pre' || env == 'sit') ? 'cnsuning.com' : 'suning.com';
    document.domain = window.domain = domain_name;
    $('#filechooser').AjaxFileUpload({
        action: baseurl + '/post/save/fileUpload.do',
        onComplete: uploadCallback
    })
})

function uploadCallback(data) {
    if (!(+data.result)) {
        var ele = $('<li class="uploadfuck"><img></li>');
        ele.find('img').attr('src', data.url).show();
        $('.sendimg').find('ul').append(ele);
        if ($('.uploadfuck').length == 5) {
            $('.sendimg').find('a').hide();
        }
    }
    else {
        alert(data.errorMsg)
    }

}